﻿/*jslint */
/*global openDatabase: true, $: true */
"use strict";

//

var Page_Settings = (function () {

    var contactID;

    function errorHandler(transaction, error) {
        console.log('Oops. Error was ' + error.message + ' (Code ' + error.code + ')');
        return true;
    }

    function errorHandlerSimple(error) {
        console.log('Oops. Error was ' + error.message + ' (Code ' + error.code + ')');
        return true;
    }


    function populateDisplay_SettingsInfo(oSingleSettings) {
        console.log("entering: populateDisplay_SettingsInfo");
    }

    /*populateDisplay_ContactInfo*/
    function populateDisplay_ContactInfo(oSingleContact) {
        //console.log("entering: populateDisplay_ContactInfo");
        ///********************************/
        ////some weird object
        ///********************************/
        //var oDisplay = new Object();
        //oDisplay.lastContactedDate = '';
        //oDisplay.lastContactedInfo = '';
        //oDisplay.flagColor = '';
        ///********************************/

        ////handle the picture
        //oDisplay.picturePath = oSingleContact.picturePath;
        //if (!oSingleContact.picturePath) {
        //    //avatar picture
        //    oDisplay.picturePath = 'images/live/avatar_noPic.png';
        //}

        //$("#contactDetails_Pic").attr("src", oDisplay.picturePath);

        ////TODO:Move this function into a utility
        //calculate_TimeSinceLastContacted(oSingleContact, oDisplay)

        ////title
        //$('#contactDetails_HeaderName').html(oSingleContact.displayName);
        //$('#contactDetails_name').html(oSingleContact.displayName);
        //$('#contactDetails_lastcontated').html(oDisplay.lastContactedInfo);
    }

    //callback - success - load settings - from localStorage
    function callback_success_load_Settings_fromLocalStorage(oSingleSettings) {
        console.log("entering: callback_success_load_Settings_fromLocalStorage");

        console.log(oSingleSettings);
        $("#settings_person_name").val(oSingleSettings.UName);
        $("#settings_person_email").val(oSingleSettings.UEmail);
        $("#settings_autosynchFrequency").val(oSingleSettings.AutoSyncFrequency);
        $("#settings_uncategorizedContactTo").val(oSingleSettings.UncategorizedContactTo);
        //we populate the settings value with the item found

      
    }
   
    //callback - success - load settings - from localStorage
    function callback_error_load_Settings_fromLocalStorage(oSingleSettings) {
        console.log("entering: callback_success_load_Settings_fromLocalStorage");

        //we populate the settings value with the item found

    }

    



    
    /***************************************************************************/
    /* Beginning Return of Exposed Functions                                   */
    /***************************************************************************/

    return {

        //********************************************************************/
        //Init function
        //********************************************************************/
        fn_init: function () {

            //start the database
            dataService.startDatabase();

            console.log('entering: fn_init');

            //show message
            $("#loadingContainer").css("display", "inline");

            //get the record from the table
            this.fn_load_Settings_fromLocalStorage();

            //hide message
            $("#loadingContainer").css("display", "none");

        },

       
        //fn_load_Settings_fromLocalStorage
        fn_load_Settings_fromLocalStorage : function(){
            console.log('entering fn_load_Settings_fromLocalStorage');

            //get the setting from the local table
            dataService.get_Settings_FromLocalStorage(
                    callback_success_load_Settings_fromLocalStorage,
                    callback_error_load_Settings_fromLocalStorage);
        }, //end load settings

            
       //Save Settings
        fn_Settings_SaveUpdate: function () {
            console.log('entering: fn_Settings_SaveUpdate');

           
            var oSettings = new Object();
            oSettings.username = $("#settings_person_name").val();
            oSettings.useremail = $("#settings_person_email").val();
            oSettings.autosyncFrequency = $("#settings_autosynchFrequency").val();
            oSettings.uncategorizedContactTo = $("#settings_uncategorizedContactTo").val();

            //alert(oSettings.username);
            //alert(oSettings.useremail);
            //alert(oSettings.autosyncFrequency);
            //alert(oSettings.uncategorizedContactTo);


            dataService.update_Table_UCred_WithSettings(oSettings,
                                                            function successCB() {
                                                                //alert('yes sir');
                                                                $.jGrowl('Updated Successfully!', { theme: 'success'})
                                                            },
                                                            function errorCB() {
                                                                $.jGrowl('Update Failed!', { theme: 'failure' })
                                                            }
                                                       );

        } //end save settings



    };
}());
